package com.xingxue.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.xingxue.entity.Category;
import com.xingxue.entity.vo.*;
import com.xingxue.mapper.CategoryMapper;

@RestController
public class CategoryController {

    @Autowired
    public CategoryMapper categoryMapper;

    @GetMapping("/category/datagrid")
    public DataGridVo dataGrid(@RequestParam(defaultValue="1") int page, @RequestParam(defaultValue="10") int rows,
        CategoryVo vo) {
        List<Category> list = categoryMapper.findByVo(vo);
        int total = categoryMapper.findCountByVo(vo);
        return new DataGridVo(list, total);
    }

    @PostMapping("/category/save")
    public Map<String,Object> save(Category category){
        
        categoryMapper.save(category);
        Map<String,Object> map = new HashMap<>();
        map.put("success", true);
        return map;
    }

    @PostMapping("/category/update")
    public Map<String,Object> update(Category category){
        
        categoryMapper.update(category);
        Map<String,Object> map = new HashMap<>();
        map.put("success", true);
        return map;
    }

    @GetMapping("/category/findById")
    public Category findById(int id){
        return categoryMapper.findById(id);
    }

    @GetMapping("/category/deleteById")
    public Map<String,Object> delete(int id){
        categoryMapper.deleteById(id);
        Map<String,Object> map = new HashMap<>();
        map.put("success", true);
        return map;
    }

    @GetMapping("/category/deleteByIds")
    public Map<String,Object> delete(@RequestParam List<java.lang.Integer> id){
        categoryMapper.deleteByIds(id);
        Map<String,Object> map = new HashMap<>();
        map.put("success", true);
        return map;
    }
    

}